package com.freak.kotlinfitnessserver.activity.fragment.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.freak.kotlinfitnessserver.room.DeviceInfoData
import com.freak.kotlinfitnessserver.room.dao.DeviceInfoDataDao
import com.freak.kotlinfitnessserver.util.LogUtil
import com.freak.kotlinfitnessserver.util.ToastUtil
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch

class DeviceInfoViewModel(private val deviceInfoDataDao: DeviceInfoDataDao) : ViewModel() {
    companion object {
        const val TAG: String = "DeviceInfoViewModel"
    }

    fun insertDeviceInfo(item: DeviceInfoData) {
        LogUtil.i(TAG, "insertDeviceInfo")
        viewModelScope.launch {
            val isSuccess = deviceInfoDataDao.insert(item)
            if (isSuccess > 0) {
                ToastUtil.toastShort("插入成功")
            } else {
                ToastUtil.toastShort("插入失败")
            }
        }
    }

    fun updateDeviceInfo(item: DeviceInfoData) {
        LogUtil.i(TAG, "updateDeviceInfo")
        viewModelScope.launch {
            val isSuccess = deviceInfoDataDao.update(item)
            if (isSuccess > 0) {
                ToastUtil.toastShort("更新成功")
            } else {
                ToastUtil.toastShort("更新失败")
            }
        }
    }

    fun getDeviceInfoList(): List<DeviceInfoData> {
        val resultList = mutableListOf<DeviceInfoData>()
        viewModelScope.launch {
            val flow: Flow<List<DeviceInfoData>> = deviceInfoDataDao.getDeviceInfoDataList()
            flow.collect { list ->
                resultList.addAll(list)
            }
        }
        return resultList
    }

    /**
     * 异步获取，需注册回调或者注册观察者
     */
    fun getFlowDeviceInfo(deviceType: String) {
        var deviceInfoData: DeviceInfoData? = null
        val job = viewModelScope.launch {
            val flow: Flow<DeviceInfoData> =
                deviceInfoDataDao.getFlowDeviceInfoData(deviceType)
            flow.collect {
                deviceInfoData = it
                LogUtil.i(TAG, "deviceInfoData = ${deviceInfoData.toString()}")
            }
        }
    }

    suspend fun getDeviceInfo(deviceType: String): DeviceInfoData? {
        var deviceInfoData: DeviceInfoData? = null
        val job = viewModelScope.launch {
            deviceInfoData = deviceInfoDataDao.getDeviceInfoData(deviceType)
            LogUtil.i(TAG, "deviceInfoData =  ${deviceInfoData.toString()}")
        }
        job.join()
        return deviceInfoData
    }

    class DeviceInfoViewModelFactory(private val deviceInfoDataDao: DeviceInfoDataDao) :
        ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            if (modelClass.isAssignableFrom(DeviceInfoViewModel::class.java)) {
                @Suppress("UNCHECKED_CAST")
                return DeviceInfoViewModel(deviceInfoDataDao) as T
            }
            throw IllegalArgumentException("Unknown ViewModel class")
        }
    }
}